package calculate24.play.common.card;

import android.view.View;

import calculate24.play.R;

public class Card {
    public DrawTextImageView view;
    public int number;
    private boolean picked = false;
    private OnPickListener onPickListener;

    public boolean isPicked() {
        return picked;
    }

    public void showNumber(int number) {
        this.number = number;
        view.setDrawText("" + this.number);
        view.setImageResource(R.drawable.white_card);
    }
    
    public Card(DrawTextImageView view, int number) {
        this.view = view;
        this.number = number;
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(picked) {
                    drop();
                } else {
                    pick();
                }
            }
        });
    }

    public boolean pick() {
        if(!picked) {
            if (onPickListener.canPick(Card.this)) {
                picked = true;
                if(onPickListener != null) {
                    onPickListener.onPick(Card.this);
                    return true;
                }
            }
            return false;
        }
        return false;
    }

    public boolean drop() {
        if(picked) {
            picked = false;
            if(onPickListener != null) {
                onPickListener.onDrop(Card.this);
            }
            return true;
        }
        return false;
    }

    public void setOnPickListener(OnPickListener listener) {
        onPickListener = listener;
    }

    public static interface OnPickListener {
        void onPick(Card card);
        void onDrop(Card card);
        boolean canPick(Card card);
    }

}
